import 'dart:math';

import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatefulWidget {
  @override
  _HYHomePageState createState() => _HYHomePageState();
}

class _HYHomePageState extends State<HYHomePage>
    with SingleTickerProviderStateMixin {
  // 创建AnimationController
  AnimationController _controller;
  Animation _animation;
  Animation _sizeAnimation;
  Animation _colorAnimation;
  Animation _opacityAnimation;
  Animation _radiansAnimation;

  @override
  void initState() {
    super.initState();

    // 1.创建AnimationController
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
      // lowerBound: 50,
      // upperBound: 150,
    );

    // 2.设置Curve的值
    _animation = CurvedAnimation(parent: _controller, curve: Curves.linear);

    //3. Tween
    //3.1 创建size的Tween
    _sizeAnimation = Tween(begin: 10.0, end: 200.0).animate(_animation);
    //3.2 创建color的Tween
    _colorAnimation =
        ColorTween(begin: Colors.red, end: Colors.purple).animate(_animation);
    //3.3 创建opacity的Tween
    _opacityAnimation = Tween(begin: 0.0, end: 1.0).animate(_animation);
    //3.3 创建radians的Tween
    _radiansAnimation = Tween(begin: 0.0, end: 2 * pi).animate(_animation);

    // 监听动画值的改变
    // _controller.addListener(() {
    //   setState(() {});
    // });

    // 监听动画的状态改变
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    print('执行_HYHomePageState的build方法');

    /// 1.大小变化的动画
    /// 2.颜色变化的动画
    /// 3.透明度变化
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
            child: AnimatedBuilder(
              animation: _controller,
              builder: (ctx, child) {
                return Opacity(
                  opacity: _opacityAnimation.value,
                  child: Transform(
                    transform: Matrix4.rotationZ(_radiansAnimation.value),
                    alignment: Alignment.center,
                    child: Container(
                      width: _sizeAnimation.value,
                      height: _sizeAnimation.value,
                      // transform: Matrix4.rotationZ(pi/4),
                      color: _colorAnimation.value,
                    ),
                  ),
                );
              },
            )),
        backgroundColor: Colors.white,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.play_arrow),
            backgroundColor: Colors.teal,
            onPressed: () {
              // _controller.forward();

              if (_controller.isAnimating) {
                _controller.stop();
                print(_controller.status);
              } else if (_controller.status == AnimationStatus.forward) {
                print(_controller.status);
                _controller.forward();
              } else if (_controller.status == AnimationStatus.reverse) {
                _controller.reverse();
              } else {
                _controller.forward();
              }
            }),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
